<?php

namespace App\Utils;

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

abstract class CreateMigration extends Migration
{
    protected $tableName;
    protected $comment;

    abstract function columns(Blueprint $table);

    abstract function tableInfo();

    public function __construct()
    {
        list($name, $comment) = $this->tableInfo();
        $this->tableName = $name;
        $this->comment = $comment;
    }

    public function getData()
    {
        return [];
    }

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

        Schema::create($this->tableName, function (Blueprint $table) {
            $table->id()->comment('自增ID');
            $this->columns($table);
            $table->softDeletes();
            $table->timestamps();
            $table->engine = 'InnoDB';
        });
        DB::statement("ALTER TABLE `$this->tableName` comment '$this->comment'");
        $data = $this->getData();
        if (count($data)) {
            DB::table($this->tableName)->insert($data);
        }
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }
}
